#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

void *thread_function(void *arg);
char message[] = "THREAD_TEST";

int main() {

    int res;                // 保存线程创建返回值
    pthread_t a_thread;    // 保存线程标识信息
    void *thread_result;    // 线程运行返回值

    res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
    if (0 != res) {
        perror("线程创建失败");
        exit(EXIT_FAILURE);
    }
    printf("等待线程结束...\n");
    res = pthread_join(a_thread, &thread_result);
    if (0 != res) {
        perror("等待线程结束失败");
        exit(EXIT_FAILURE);
    }
    printf("线程已结束,返回值:%s\n", (char *)thread_result);
    printf("Message 值:%s\n", message);

    exit(EXIT_SUCCESS);

}

void *thread_function(void *arg) {
    printf("线程正在运行,参数为:%s\n", (char *)arg);
    sleep(3);
    strcpy(message, "线程修改");
    pthread_exit("线程执行完毕");

}